package com.king.client.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.king.client.ChannelEpisodeActivity;
import com.king.client.R;
import com.king.client.net.ImageDownloader;
import com.king.client.net.ImageDownloader.ImageDownloaderCallback;

import framework.parser.rss.Rss;

public class HotAdapter extends BaseAdapter {
	private static final String TAG = "HotItemAdapter";
	private LayoutInflater mInflater;
	private Context mContext = null;
	private Rss mRssContent = null;
	private ListView listView;

	// public int count = 10;

	public HotAdapter(Context context, ListView listView, Rss rss) {
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
		this.mRssContent = rss;
		this.listView = listView;
	}

	public int getCount() {
		if (mRssContent != null && !mRssContent.getChannels().isEmpty()) {
			return mRssContent.getChannels().get(0).getItems().size();
		} else {
			return 0;
		}
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		Bitmap itemIcon = null;
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.top_item_view, null);
			holder = new ViewHolder();
			holder.mItemIcon = (ImageView) convertView
					.findViewById(R.id.top_item_icon);

			// holder.mIcon = (ImageView) convertView
			convertView.setClickable(true);

			convertView.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					String vid = (String) v.getTag(R.id.vid);
					String imageLink = (String) v.getTag(R.id.imageLink);
					String mediaLink = (String) v.getTag(R.id.mediaLink);

					String vtitle = (String) v.getTag(R.id.item_title);
					String vtime = (String) v.getTag(R.id.item_pubdate);
					String vtimes = (String) v.getTag(R.id.item_viewed_times);
					// Intent intent = new Intent(ChannelActivity.this,
					// VideoPlayer.class);
					Intent intent = new Intent(mContext,
							ChannelEpisodeActivity.class);
					intent.putExtra("imageLink", imageLink);
					intent.putExtra("mediaLink", mediaLink);
					intent.putExtra("vid", vid);

					intent.putExtra("vtitle", vtitle);
					intent.putExtra("vtime", vtime);
					intent.putExtra("vtimes", vtimes);

					mContext.startActivity(intent);

				}
			});
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// holder
		// item_time

		// holder.mItemTitle.setTextColor(0xff004980);

		String vid = mRssContent.getChannels().get(0).getItems().get(position)
				.getValues().get(0);
		String mediaLink = mRssContent.getChannels().get(0).getItems().get(
				position).getValues().get(2);
		String imageLink = mRssContent.getChannels().get(0).getItems().get(
				position).getValues().get(4);

		convertView.setTag(R.id.mediaLink, mediaLink);
		convertView.setTag(R.id.imageLink, imageLink);
		convertView.setTag(R.id.vid, vid);

		itemIcon = ImageDownloader.loadImage(mContext, imageLink, 0, position,
				new ImageDownloaderCallback() {
					public void imageLoaded(Bitmap bitmap, final int channelId,
							final int itemId, String imageUrl) {
						ImageView iv = (ImageView) listView
								.findViewWithTag(mRssContent.getChannels().get(
										0).getItems().get(position).getValues()
										.get(4));
						if (iv != null) {
							if (bitmap != null) {
								iv.setImageBitmap(bitmap);
							}
						}
						// if (bitmap != null) {
						// notifyDataSetChanged();
						// }
					}
				});

		if (itemIcon == null) {
			Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(mContext.getResources(),
							R.drawable.item_default_icon), 110, 80, true);
			holder.mItemIcon.setImageBitmap(bitmap);
		} else {
			holder.mItemIcon.setImageBitmap(itemIcon);
		}

		return convertView;
	}

	static class ViewHolder {
		ImageView mItemIcon;

	}
}
